package com.yic.module.wms.service.warehousearea;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yic.module.system.enums.dict.SystemBooleanEnum;
import com.yic.module.wms.dal.dataobject.warehouse.WarehouseDO;
import com.yic.module.wms.dal.dataobject.warehouselocation.WarehouseLocationDO;
import com.yic.module.wms.dal.redis.RedisKeyConstants;
import com.yic.module.wms.service.warehouse.WarehouseService;
import com.yic.module.wms.service.warehouselocation.WarehouseLocationService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.wms.controller.admin.warehousearea.vo.*;
import com.yic.module.wms.dal.dataobject.warehousearea.WarehouseAreaDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.wms.convert.warehousearea.WarehouseAreaConvert;
import com.yic.module.wms.dal.mysql.warehousearea.WarehouseAreaMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.wms.enums.ErrorCodeConstants.*;

/**
 * 库区 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class WarehouseAreaServiceImpl implements WarehouseAreaService {

    @Resource
    private WarehouseAreaMapper warehouseAreaMapper;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    @Lazy
    private WarehouseLocationService warehouseLocationService;

    @Override
    public Long createWarehouseArea(WarehouseAreaCreateReqVO createReqVO) {
        validateCreateOrUpdate(null, createReqVO.getCode(), createReqVO.getWarehouseId());
        // 插入
        WarehouseAreaDO warehouseArea = WarehouseAreaConvert.INSTANCE.convert(createReqVO);
        warehouseAreaMapper.insert(warehouseArea);
        // 返回
        return warehouseArea.getId();
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.WAREHOUSE_AREA, key = "#updateReqVO.id")
    public void updateWarehouseArea(WarehouseAreaUpdateReqVO updateReqVO) {
        // 校验存在
        validateCreateOrUpdate(updateReqVO.getId(), updateReqVO.getCode(), updateReqVO.getWarehouseId());
        validateWarehouseLocationExists(updateReqVO.getId());
        // 更新
        WarehouseAreaDO updateObj = WarehouseAreaConvert.INSTANCE.convert(updateReqVO);
        warehouseAreaMapper.updateById(updateObj);
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.WAREHOUSE_AREA, key = "#id")
    public void deleteWarehouseArea(Long id) {
        // 校验存在
        validateWarehouseAreaExists(id);
        validateWarehouseLocationExists(id);
        // 删除
        warehouseAreaMapper.deleteById(id);
    }

    private void validateCreateOrUpdate(Long id, String code, Long warehouseId){
        validateCodeExists(id, code);
        validateOpenArea(warehouseId);
    }

    private void validateCodeExists(Long id, String code) {
        WarehouseAreaDO warehouseAreaDO = warehouseAreaMapper.selectByCode(code);
        if (ObjectUtil.isEmpty(warehouseAreaDO)) {
            return;
        }
        if (id == null) {
            throw exception(WAREHOUSE_AREA_CODE_EXISTS);
        }
        if (!id.equals(warehouseAreaDO.getId())) {
            throw exception(WAREHOUSE_AREA_CODE_EXISTS);
        }
    }

    private void validateOpenArea(Long warehouseId) {
        WarehouseDO warehouse = warehouseService.getWarehouse(warehouseId);
        if (warehouse.getOpenArea().equals(SystemBooleanEnum.NO.getSystemBoolean())) {
            throw exception(WAREHOUSE_AREA_NOT_CHANGE);
        }
    }

    private void validateWarehouseAreaExists(Long id) {
        if (warehouseAreaMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_AREA_NOT_EXISTS);
        }
    }

    private void validateWarehouseLocationExists(Long id) {
        List<WarehouseLocationDO> warehouseLocationDOS = warehouseLocationService.getWarehouseLocationListByAreaId(id);
        if (CollectionUtil.isNotEmpty(warehouseLocationDOS)) {
            throw exception(WAREHOUSE_AREA_NOT_DEL_EDIT);
        }
    }

    @Override
    @Cacheable(cacheNames = RedisKeyConstants.WAREHOUSE_AREA, key = "#id")
    public WarehouseAreaDO getWarehouseArea(Long id) {
        return warehouseAreaMapper.selectById(id);
    }

    @Override
    public List<WarehouseAreaDO> getWarehouseAreaList(Collection<Long> ids) {
        return warehouseAreaMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WarehouseAreaDO> getWarehouseAreaPage(WarehouseAreaPageReqVO pageReqVO) {
        return warehouseAreaMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WarehouseAreaDO> getWarehouseAreaList(WarehouseAreaExportReqVO exportReqVO) {
        return warehouseAreaMapper.selectList(exportReqVO);
    }

    @Override
    public List<WarehouseAreaDO> getWarehouseAreaListByWarehouseId(Long warehouseId) {
        return warehouseAreaMapper.selectListByWarehouseId(warehouseId);
    }

    @Override
    public List<WarehouseAreaDO> getWarehouseAreaList(Collection<Long> ids, Long warehouseId, Collection<Integer> status) {
        return warehouseAreaMapper.selectList(ids, warehouseId, status);
    }

}
